import {MenuInfoService} from "./MenuInfoService";


var picInfoDaoObj = require('../dao/PicInfoDao.js');
var commonUtil = require('./../util/commonUtil.js');

var response = require('./../util/response.js');

var picInfoDao = new picInfoDaoObj.PicInfoDao();

/**
 * { function_description }
 *
 * @class      UserService (name)
 */
export function PicInfoService() {
    console.info('into PicInfoService...');
}

PicInfoService.prototype.constructor = PicInfoService;


/**
 * { 注册 }
 *
 * @param      {<type>}    params  The parameters
 * @return     {response}  { description_of_the_return_value }
 */
PicInfoService.prototype.savePicInfo = function(params) {
    for (let i in params) {
        if (params[i] == null || undefined || '') {
            this.errorMsg = '参数不齐全!';
            return this.errorMsg;
        }
    }
    var params2 = {};
    /*此处赋值要对应SQL语句中的字段顺序,不然会段与值之间发生错位*/
    params2.picName = params.picName;
    params2.picUrl = params.picUrl;
    params2.picSize = params.picSize;
    params2.picMneuId = params.picMneuId;
    params2.createTime = params.createTime; //默认值
    try {
        picInfoDao.savePicInfo1(params2);
    } catch (error) {
        return new response.response(error.errno, error.code, 0);
    }

    return new response.response(200, 'OK', 1);
};


PicInfoService.prototype.batchInsert = function(params) {
    if(params == null || params == '' || params.length<0){
        this.errorMsg = '参数不齐全!';
        return this.errorMsg;
    }
    var paramRes=[];
    var valueArr=[];
    for(var i=0;i<params.length;i++){
        valueArr = Object.values(params[i]);
        paramRes.push(valueArr);
    }
    try {
        picInfoDao.batchInsert(paramRes);
    } catch (error) {
        return new response.response(error.errno, error.code, 0);
    }

    return new response.response(200, 'OK', 1);
};



/**
 * 修改图片信息
 *
 * @param      {<type>}    user    The user
 * @return     {response}  { description_of_the_return_value }
 */
PicInfoService.prototype.updatePicInfo = (picInfo) => {
    try {
        picInfoDao.updatesById(picInfo);
    } catch (err) {
        return new response.response(505, err, '');
    }
    var data = picInfoDao.getByField({ name: 'id', value: picInfo.id });
    return new response.response(200, '', data);
}

/**
 * 获取图片列表
 *
 * @return     {<type>}  { description_of_the_return_value }
 */
PicInfoService.prototype.findAllPic = function() {
    return picInfoDao.getPicInfoList();
}



/**
 * 根据属性获取图片 id 或者menuId
 * 传参：{ name: 'id', value: id的值} { name: 'pic_menu_id', value: 菜单的值}
 * @param      {string}  id      The identifier
 * @return     {<type>}  One user.
 */
PicInfoService.prototype.getPicInfoByField = function(param) {
    return picInfoDao.getByField(param);
};

/**
 * 获取图片列表
 *
 * @return     {<type>}  { description_of_the_return_value }
 */
PicInfoService.prototype.getMenuAndPicByMenuId = function(params) {
    for (let i in params) {
        if (params[i] == null || undefined || '') {
            this.errorMsg = '参数不齐全!';
            return this.errorMsg;
        }
    }
    var pageNum = (params.pageNum-1)*params.pageSize;
    params.pageNum = pageNum;

    var total = picInfoDao.getTotalNum(params);
    var res ={
        data:picInfoDao.getMenuAndPicByMenuId(params),
        total: total
    }
    return new response.response(200, '查询成功', res);
}

/**
 * 获取图片
 * @param params
 * @returns {string|response}
 */
PicInfoService.prototype.getAllPic = function(params) {
    for (let i in params) {
        if (params[i] == null || undefined || '') {
            this.errorMsg = '参数不齐全!';
            return this.errorMsg;
        }
    }
    return new response.response(200, '查询成功', picInfoDao.getAllPic(params));
}

/**
 * 获取菜单
 * @param params
 * @returns {string|response}
 */
PicInfoService.prototype.getAllMenu = function() {
    return new response.response(200, '查询成功', picInfoDao.getAllMenu());
}

/**
 * { 删除图片信息 }
 *
 * @param      {<type>}    params  The parameters
 * @return     {response}  { description_of_the_return_value }
 */
PicInfoService.prototype.deletePicInfos = function(params) {
    var result = 0;
    try {
        if ((params == null || undefined || '')||params.length<=0) {
            this.errorMsg = 'id不能为空!';
            return this.errorMsg;
        }
        var menuList = [];
        var picList = [];
        for(var i =0;i<params.length;i++){
            if(params[i].type==0){
                menuList.push(params[i].id);
            }
            if(params[i].type==1){
                picList.push(params[i].id);
            }
        }

        //删除图片
        if(picList.length>0){
            var picParam = { column: 'id', value: picList };
            picInfoDao.deleteByIds(picParam);
        }
        //删除菜单
        if(menuList.length>0){
            picParam.column = 'pic_menu_id';
            picParam.value = menuList;
            result = picInfoDao.deleteByIds(picParam);//删除菜单对应的图片
        }
    } catch (error) {
        return new response.response(error.errno, error.code, 0);
    }
    return new response.response(200, 'OK', result);
}

/**
 *
 * @returns {*}
 */
PicInfoService.prototype.getMenuByNoParam = function(params) {
    return new Promise((reslove, reject) => {
        var treeArr =[];

        picInfoDao.getAllMenu().then(res =>{
            console.log("paramparam",params);
            treeArr=  getChildlist(res,params,"parent_id");
            treeArr.push(params);
            console.log("treeArrtreeArrtreeArr",treeArr);
            reslove(picInfoDao.getMenuByNoParam(treeArr));
        });
    });
};

function getChildlist(list, id, pName){//数组 id  父级节点名称
    let blist = []
    for(let i=0,ilens=list.length,d; i<ilens ;i++){
        d = list[i]
        if(d[pName] && d[pName] == id){
            blist.push(d.id)
            const childs =getChildlist(list, d.id, pName)
            if(childs.length > 0){
                blist = blist.concat(childs)
            }
        }
    }
    return blist
}

